package com.allprs.common.utils;

import com.allprs.common.utils.uuid.IdUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.charset.Charset;
import java.text.DecimalFormat;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * 公共方法抽取
 */
public class CommonUtils {

    private static final Logger log = LoggerFactory.getLogger(CommonUtils.class);


    /**
     * 获取资源类型
     * @param key
     * @return
     */
    public static String getResourceTypeDesc(String key) {
        if("1000008".equals(key)){
            return "地形数据";
        }else if("2000004".equals(key)){
            return "矢量数据";
        }else if("5000010".equals(key) || "5000011".equals(key) || "5000012".equals(key)){
            return "精细模型";
        }else if("12000008".equals(key)){
            return "影像数据";
        }else if("13000006".equals(key)){
            return "点云数据";
        }else if("14000003".equals(key)){
            return "BIM模型";
        }else if("16000002".equals(key)){
            return "倾斜模型";
        }else if("17000002".equals(key)){
            return "建筑白模";
        }else{
            return null;
        }
    }

    /**
     * 根据服务类型取服务格式
     * @param resourceType
     * @return
     */
    public static String getResourctForm(String resourceType) {
        if("1000008".equals(resourceType)){
            return "地形切片";
        }else if("2000004".equals(resourceType)){
            return "MVT";
        }else if("5000010".equals(resourceType)){
            return "gltf";
        }else if("5000011".equals(resourceType)){
            return "glb";
        }else if("5000012".equals(resourceType)){
            return "3DTiles";
        }else if("12000008".equals(resourceType)){
            return "影像切片";
        }else if("13000006".equals(resourceType)){
            return "3Dtiles";
        }else if("14000003".equals(resourceType)){
            return "3Dtiles";
        }else if("16000002".equals(resourceType)){
            return "3Dtiles";
        }else if("17000002".equals(resourceType)){
            return "矢量瓦片";
        }else{
            return null;
        }
    }

    //根据大类获取小类
    public static List<Map<String, String>> getFormByType(String type) {
        List<Map<String, String>> resultList = new ArrayList<>();
        if("1".equals(type)){
            Map<String, String> map = new HashMap<>();
            map.put("formName","地形切片");
            map.put("formVale","1000008");
            resultList.add(map);
        }else if("2".equals(type)){
            Map<String, String> map = new HashMap<>();
            map.put("formName","MVT");
            map.put("formVale","2000004");
            resultList.add(map);
        }else if("5".equals(type)){
            Map<String, String> map = new HashMap<>();
            map.put("formName","gltf");
            map.put("formVale","5000010");
            resultList.add(map);
            Map<String, String> map1 = new HashMap<>();
            map1.put("formName","glb");
            map1.put("formVale","5000011");
            resultList.add(map1);
            Map<String, String> map2 = new HashMap<>();
            map2.put("formName","3DTiles");
            map2.put("formVale","5000012");
            resultList.add(map2);

        }else if("12".equals(type)){
            Map<String, String> map = new HashMap<>();
            map.put("formName","影像切片");
            map.put("formVale","12000008");
            resultList.add(map);
        }else if("13".equals(type)){
            Map<String, String> map = new HashMap<>();
            map.put("formName","3Dtiles");
            map.put("formVale","13000006");
            resultList.add(map);
        }else if("14".equals(type)){
            Map<String, String> map = new HashMap<>();
            map.put("formName","3Dtiles");
            map.put("formVale","14000003");
            resultList.add(map);
        }else if("16".equals(type)){
            Map<String, String> map = new HashMap<>();
            map.put("formName","3Dtiles");
            map.put("formVale","16000002");
            resultList.add(map);
        }else if("17".equals(type)){
            Map<String, String> map = new HashMap<>();
            map.put("formName","矢量瓦片");
            map.put("formVale","17000002");
            resultList.add(map);
        }
        return resultList;
    }

    public static String getServiceSupertype(String serviceType) {
        if("1000008".equals(serviceType)){
            return "1";
        }else if("2000004".equals(serviceType)){
            return "2";
        }else if("5000010".equals(serviceType) || "5000011".equals(serviceType) || "5000012".equals(serviceType)){
            return "5";
        }else if("12000008".equals(serviceType)){
            return "12";
        }else if("13000006".equals(serviceType)){
            return "13";
        }else if("14000003".equals(serviceType)){
            return "14";
        }else if("16000002".equals(serviceType)){
            return "16";
        }else if("17000002".equals(serviceType)){
            return "17";
        }else{
            return null;
        }
    }

    /**
     * 解压压缩包
     *
     * @param zipFile
     * @param descDir
     * @throws IOException
     */
    public static void upZip(File zipFile, String descDir) throws IOException {
        File destFile = new File(descDir);
        if (!destFile.exists()) {
            destFile.mkdirs();
        }
        // 解决zip文件中有中文目录或者中文文件
        ZipFile zip = new ZipFile(zipFile, Charset.forName("GBK"));
        for (Enumeration entries = zip.entries(); entries.hasMoreElements(); ) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            InputStream in = zip.getInputStream(entry);
            String curEntryName = entry.getName();
            // 判断文件名路径是否存在文件夹
            int endIndex = curEntryName.lastIndexOf('/');
            // 替换
            String outPath = (descDir + curEntryName).replaceAll("\\*", "/");
            if (endIndex != -1) {
                File file = new File(outPath.substring(0, outPath.lastIndexOf("/")));
                if (!file.exists()) {
                    file.mkdirs();
                }
            }

            // 判断文件全路径是否为文件夹，如果上面已经上传，不需要解压
            File outFile = new File(outPath);
            if (outFile.isDirectory()) {
                continue;
            }
            OutputStream out = new FileOutputStream(outPath);
            byte[] buf1 = new byte[1024];
            int len;
            while ((len = in.read(buf1)) > 0) {
                out.write(buf1, 0, len);
            }
            in.close();
            out.close();
        }
    }

    public static File transferToFile(MultipartFile multipartFile) {
        File file = null;
        try {
            String originalFilename = multipartFile.getOriginalFilename();
            String[] fileName = originalFilename.split("\\.");
            file = File.createTempFile(IdUtils.fastSimpleUUID(), fileName[1]);
            multipartFile.transferTo(file);
            file.deleteOnExit();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

    public static double getMbByBytes(long size) {
        DecimalFormat format = new DecimalFormat("###.0");
        double i = (size / (1024.0 * 1024.0));
        return Double.valueOf(format.format(i)) ;
    }


    /**
     * 删除临时解压文件夹
     * @param file
     */
    public static void deleteZipDirectory(File file) {
        if (file.exists()) {
            if (file.isFile()) {
                // 删除文件
                file.delete();
            } else {
                // 目标为文件夹时读取文件夹内所有文件
                File[] files = file.listFiles();
                for (File childFile : files) {
                    // 递归删除文件
                    deleteZipDirectory(childFile);
                }
            }
            file.delete();
        }
    }


    /**
     * 读取压缩后的所有样本
     *
     * @param dirFile
     * @return
     */
    public static List<File> getAllFile(File dirFile) {
        List<File> fileList = new ArrayList<>();
        File[] childrenFiles = dirFile.listFiles();
        if (Objects.isNull(childrenFiles) || childrenFiles.length == 0) {
            return fileList;
        }
        for (File childFile : childrenFiles) {
            if (childFile.isFile()) {
                // 如果是文件，直接添加到集合
                fileList.add(childFile);
            } else {
                // 如果是文件夹，将其内部文件添加至集合
                List<File> cFiles = getAllFile(childFile);
                if (cFiles.isEmpty()) {
                    continue;
                }
                fileList.addAll(cFiles);
            }
        }
        return fileList;
    }
}
